var add,setParam,paramIssue,changeCtrl, singleCmd, cmdType;
require(['select2', 'date','validator','jquery-form'], function() {
	var $formData = $('#formData'),
		paramName,
		paramValue,
		sn,
		paramNameData,
		paramValueData,
		snData,
		$endUl = $('#formData'),
		carId,
		lineId;
	$formData.initForm();
	// var nodes=commonFun.getLeftTreeSelectedNodes();

	setParam = function (paramIssue){
		$.ajaxExt({
			url :"/upgrade/getIssuedParam.htm",
			data : {'paramIssue' : paramIssue}
		}).done(function(result){
			if(result){
				var newUl= "<ul class='form-grid form-grid2 form-with-select form-grid-140 clearfix'><li class='li-select'><label class='label-text'>"
					+"<span class='red-star'>*</span>版本名称：</label><input class='input-text' type='text' id='vrsnName' name='vrsnName'  data-validator='{required:true}'>"
					+"</li><li class='li-select'><label class='label-text'>备注：</label><input class='input-text' type='text' id='vrsnName' name='vrsnName'></li>";
				if(paramIssue == '12'){
					var radioHtml = "<li class='li-textarea mb0'>";
					var paramHtml = "";
					var selectHtml = "<li class='li-select'><label class='label-text label-require'>报警等级：</label><select id='cmdSelect' disabled='disabled'>";
					$.each(result, function(i, item){
						if(item.type == 'RADIO'){
                            radioHtml += "<label class='radio-wrap'>" + item.typeName + "<input type='radio' name='paramTypeRadio' class='cmd-radio' onclick='changeCtrl(\""+item.typeVal+"\", \""+item.paramId+"\")'"+
								('CLASSG01' == item.typeVal ? "checked='checked'" : "")
                                +"></label>";
						}else if(item.type == 'PARAM'){
							paramHtml += "<li class='li-select'><label class='label-text label-require'>" + item.typeName + "：</label>"
							+"<input class='input-text param-html' type='text' id='" + item.paramId + "' name='paramName' ></li>";
						}else if(item.type == 'SELECT'){
							selectHtml += "<option value='"+item.paramId+"'>"+item.typeName+"</option>";
						}
					});
					radioHtml += "</li>";
					paramHtml += "<input class='input-text' type='hidden' id='appUpgrade' name='appUpgrade'>";
					selectHtml += "</select></ul>";
					newUl += radioHtml + paramHtml + selectHtml;
					
				}else{
					$(result).each(function(i,item) {
						newUl+= "<li class='li-select'><input type='checkbox' class='item-select'>"
						+"<label class='label-text label-require'>" + item.paramLevel2Name + "：</label>"
						+"<input class='input-text' type='text' id='" + item.paramId + "' name='paramName' value='" + item.paramDefaultValue + "'></li>";
					});
					newUl+="<input class='input-text' type='hidden' id='appUpgrade' name='appUpgrade'></ul>";
				}
				$("#appUpgrade").val(paramIssue);
				$endUl.html(newUl);
			}
		});
		if(paramIssue=="7"){
			var type = commonFun.getLeftTreeCurrentTreeType();
			var orgIds = commonFun.getLeftTreeCheckedNodes(type);
			carId =  orgIds[0];
			$.ajaxExt({
			 url :"/baseCarInfo/getCarInfoByCarId.htm",
			 data : {'carId' : carId}
			 }).done(function(data){
			 $("#VERS000902").val(data.simCardno);
			 $("#VERS000901").val(data.eqmtCd);
			 $("#VERS000915").val(data.licPltNo);
			 });
		}
	};
	
	changeCtrl = function(id, cmdId){
		cmdType = id;
		if(id == 'CLASSG01'){
			$("#cmdSelect").attr("disabled", true);
			$(".param-html").removeAttr("readonly");
		}else if(id == 'CLASSG06'){
			$(".param-html").attr("readonly","readonly");
			$("#cmdSelect").attr("disabled", false);
		}else{
			singleCmd = cmdId;
			$("#cmdSelect").attr("disabled", true);
			$(".param-html").attr("readonly","readonly");
		}
	};

	add = function(fn) {
		addOrUpdate("/upgrade/addCanMgr.htm",fn);
	};

	function addOrUpdate(url,callBack){
		// if(nodes[0].type=="LINE"){
		// 	lineId = nodes[0].id;
		// }else{
		// 	lineId = nodes[0].parentId;
		// }
		var type = commonFun.getLeftTreeCurrentTreeType();
		var orgIds = commonFun.getLeftTreeCheckedNodes(type);
		var result = $.validator($formData);
		if(result){
			var params = [],flag="0";
			if($("#appUpgrade").val() == '12'){
				if(cmdType == 'CLASSG01'){
					$.each($(".param-html"), function(i, elem){
						params.push({
							name  : "list["+i+"].paramName",
							value : elem.id
						});
						params.push({
							name  : "list["+i+"].paramValue",
							value : elem.value
						});
						params.push({
							name  : "list["+i+"].sn",
							value : i
						});
					});
					params.push({
						name  : "vrsnName",
						value : $("#vrsnName").val()
					}/*,{
						name  : "remark",
						value :  $("#remark").val()
					}*/,{
						name  : "appUpgrade",
						value : $("#appUpgrade").val()
					},{
						name  : "orgIds",
						value : orgIds
					},{
						name  : "paramId",
						value : paramIssue
					});
				}else if(cmdType == 'CLASSG07'){
					params.push({
						name  : "list[0].paramName",
						value : singleCmd
					});
					params.push({
						name  : "list[0].paramValue",
						value : singleCmd
					});
					params.push({
						name  : "list[0].sn",
						value : 0
					});
					params.push({
						name  : "vrsnName",
						value : $("#vrsnName").val()
					}/*,{
						name  : "remark",
						value :  $("#remark").val()
					}*/,{
						name  : "appUpgrade",
						value : $("#appUpgrade").val()
					},{
						name  : "orgIds",
						value : orgIds
					},{
						name  : "paramId",
						value : paramIssue
					});
				}else{
					params.push({
						name  : "list[0].paramName",
						value : singleCmd
					});
					params.push({
						name  : "list[0].paramValue",
						value : singleCmd
					});
					params.push({
						name  : "list[0].sn",
						value : 0
					});
					params.push({
						name  : "vrsnName",
						value : $("#vrsnName").val()
					}/*,{
						name  : "remark",
						value :  $("#remark").val()
					}*/,{
						name  : "appUpgrade",
						value : $("#appUpgrade").val()
					},{
						name  : "orgIds",
						value : orgIds
					},{
						name  : "paramId",
						value : paramIssue
					});
				}
			}else{
				$(":checkbox:checked").closest("li").find(":text").each(function(i, eleDom){
					// 遍历每个被选中的复选框所在行的文本框的值
					flag="1";
					params.push({
						name  : "list["+ i + "].paramName",
						value : eleDom.id
					});
					params.push({
						name  : "list["+ i + "].paramValue",
						value : eleDom.value
					});
					params.push({
						name  : "list["+ i + "].sn",
						value : i
					});
				});
				if(flag!="1"){
					$.messager.alert("提示","请至少选择一项"+ getName($("#appUpgrade").val()));
					return;
				}
				params.push({
					name  : "vrsnName",
					value : $("#vrsnName").val()
				},{
					name  : "remark",
					value :  $("#remark").val()
				},{
					name  : "appUpgrade",
					value : $("#appUpgrade").val()
				},{
					name  : "orgIds",
					value : orgIds
				},{
					name  : "paramId",
					value : paramIssue
				});
			}
			// var mask = $.createMask();
			$.ajaxExt({
				url : url,
				data : params
			}).done(function(data){
				// mask.remove();
				callBack();
			});
		}
	}

	function getName(appUpgrade){

		if(appUpgrade=="5") {
			return "通讯参数";
		}else if(appUpgrade=="6"){
			return "外设参数";
		}else if(appUpgrade=="8"){
			return "行为参数";
		}else if(appUpgrade=="9"){
			return "线路参数";
		}else if(appUpgrade=="10"){
			return "发动机参数";
		}else if(appUpgrade=="11"){
			return "电机参数";
		}else if(appUpgrade=="7"){
			return "唯一参数";
		}else if(appUpgrade=="12"){
			return "终端控制";
		}
	}

});